import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:secend_flutter/bottom_bar/search_item_page.dart';
import 'package:secend_flutter/dao/tao_ean.dart';
import 'package:secend_flutter/plugins/bd_asr_plugin.dart';

class SpeakPage extends StatefulWidget {
  const SpeakPage({Key? key}) : super(key: key);

  @override
  _SpeakPageState createState() => _SpeakPageState();
}

class _SpeakPageState extends State<SpeakPage> with SingleTickerProviderStateMixin{
  String speakHint = '长按说话';
  String speakReslut = '';
  Animation<double>? animation;
  AnimationController? controller;

  @override
  void initState() {
    controller=AnimationController(vsync: this,duration: Duration(milliseconds: 2000));
    animation=CurvedAnimation(parent: controller!, curve: Curves.easeIn);
    animation!.addStatusListener((status) {
      if(status==AnimationStatus.completed){
        controller!.reverse();
      }else if(status==AnimationStatus.dismissed){
        controller!.forward();
      }
    });
    super.initState();

  }
  @override
  void dispose() {
    controller!.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.all(30),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _itemByTop(),
            _itemByBottom(),
          ],
        ),
      ),
    );
  }

  _itemByBottom() {
    return FractionallySizedBox(
      widthFactor: 1,
      child: Stack(
        children: [
          GestureDetector(
            onTapDown: (e) {
              _speakStart();
            },
            onTapUp: (e) {
              _speakStop();
            },
            onTapCancel: _speakStop,
            child: Center(
              child: Column(
                children: [
                  Container(
                      padding: const EdgeInsets.all(10),
                      child: Text(
                        speakHint,
                        style: TextStyle(
                          decoration: TextDecoration.none,
                            color: Colors.blue,
                            fontSize: ScreenUtil().setSp(22)),
                      )),
                  Stack(
                    children: [
                      Container(
                        height: W_SIZE,
                        width:W_SIZE,
                      ),
                      Center(
                        child: AnimateW(
                          animation: animation!,
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
          Positioned(child: GestureDetector(
            onTap: (){
              Navigator.pop(context);
            },
            child: Icon(Icons.close,size: 35,color: Colors.black,),
          ),
          right: 0,
          bottom: 20,)
        ],
      ),
    );
  }

  void _speakStart() {
    controller!.forward();
    setState(() {
      speakHint=' 识别中 ';
    });
    AsrBdManager.startAsr().then((value){
      if(value!=null &&value.length>0){
        setState(() {
          speakReslut=value;
        });
        Navigator.pop(context);
        Tiaboon.push(context, SearchPage(hideLeft: true,keyword: speakReslut,));
        print('-++++++++++++++++++++++++++'+value);
      }
    });
  }

  void _speakStop() {
    setState(() {
      speakHint='长按说话';
    });
    controller!.reset();
    controller!.stop();
    AsrBdManager.stopAsr();
  }


  _itemByTop() {
    return Column(
      children: [
        Container(
          decoration: null,
          padding: const EdgeInsets.only(top: 30,bottom: 30),
          child: const Text('你可以这样说',
          style: TextStyle(fontSize: 16,color: Colors.black54,decoration: TextDecoration.none,),
          ),
        ),
        const Text('故宫\n长城\n迪奥',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 15,
            color: Colors.black26,
            decoration: TextDecoration.none,
          ),),
        Container(
          padding: const EdgeInsets.all(20),
          child: Text(speakReslut,
          style: TextStyle(color: Colors.blue),),
        )
      ],
    );
  }
}

const double W_SIZE = 80;

class AnimateW extends AnimatedWidget {
  static final _opacityTween = Tween<double>(begin: 1, end: 0.5);
  static final _sizeTween = Tween<double>(begin: W_SIZE, end: W_SIZE - 20);

  AnimateW({required Animation<double> animation})
      : super(listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable as Animation<double>;
    return Opacity(
      opacity: _opacityTween.evaluate(animation),
      child: Container(
        height: _sizeTween.evaluate(animation),
        width: _sizeTween.evaluate(animation),
        decoration: BoxDecoration(
          color: Colors.orange,
          borderRadius: BorderRadius.circular(W_SIZE / 2),
        ),
        child: Icon(
          Icons.mic,
          color: Colors.white,
          size: 35,
        ),
      ),
    );
  }
}
